package com.run.gulimall.search.thread;

import java.util.concurrent.*;

/**
 * @Date: 2021/10/10/15:42
 * @Description:
 */
public class ThreadTest {

    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息成功！");
            return "phone.jpg";
        }, executor);

        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性信息成功！");
            return "黑色+256G";
        }, executor);

        CompletableFuture<Object> future03 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("查询商品介绍成功！");
            return "小米10";
        }, executor);

//        CompletableFuture<Void> allOf = CompletableFuture.allOf(future01, future02, future03);
//        allOf.get();
//        System.out.println("全部任务查询成功！");

        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(future01, future02, future03);
        System.out.println("完成任务 ---> "+anyOf.get());

    }

    public void main4(String[] args) throws ExecutionException, InterruptedException {
//        CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务一启动 === " + "当前线程 === " + Thread.currentThread().getId());
//            System.out.println("任务一结束。");
//            return 10 / 2;
//        }, executor);
//
//        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务二启动 === " + "当前线程 === " + Thread.currentThread().getId());
//
//            try {
//                Thread.sleep(3000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            System.out.println("任务二结束。");
//            return "hello world";
//        }, executor);

//        future01.runAfterEitherAsync(future02,()->{
//            System.out.println("任务三开始");
//        },executor);

//        future01.acceptEitherAsync(future02,(f1)->{
//            System.out.println("任务三开始 ----> " + f1);
//        },executor);

//        CompletableFuture<String> future = future01.applyToEitherAsync(future02, (result) -> {
//            return "任务三返回值 --> " + result;
//        }, executor);
//        System.out.println(future.get());
    }

    public void main3(String[] args) throws ExecutionException, InterruptedException {
//        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务一启动 === " + "当前线程 === " + Thread.currentThread().getId());
//            System.out.println("任务一结束。");
//            return 10 / 2;
//        }, executor);
//
//        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务二启动 === " + "当前线程 === " + Thread.currentThread().getId());
//            System.out.println("任务二结束。");
//            return "hello world";
//        }, executor);

//        future01.runAfterBothAsync(future02,()->{
//            System.out.println("任务三开始");
//        });

//        future01.thenAcceptBothAsync(future02,(f1,f2)->{
//            System.out.println("任务三开始 --->  "+f1+"===="+f2);
//        },executor);

//        CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
//            System.out.println("任务三开始 --->  " + f1 + "====" + f2);
//            return f1 + f2;
//        }, executor);
//        System.out.println(future.get());

    }

    public void main2(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main  =====  start");
        //  runAsync
//        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程 === " + Thread.currentThread().getId());
//            System.out.println("运行结果 === " + 10 / 2);
//        }, executor);
        //  supplyAsync
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程 === " + Thread.currentThread().getId());
//            System.out.println("运行结果 === ");
//            return 10 / 0;
//        }, executor).whenComplete((res,exception)->{
//            System.out.println("异步任务后续任务...结果是："+res+", 异常是"+exception);
//        }).exceptionally(throwable -> {
//            return 10;
//        });

//        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务一启动 === "+"当前线程 === " + Thread.currentThread().getId());
//            return 10 / 2;
//        }, executor).thenRunAsync(() -> {
//            System.out.println("任务二启动 === "+"当前线程 === " + Thread.currentThread().getId());
//        }, executor);
//        System.out.println(future.get());

//        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务一启动 === "+"当前线程 === " + Thread.currentThread().getId());
//            return 10 / 2;
//        }, executor).thenAcceptAsync((res) -> {
//            System.out.println("任务二启动 === "+"当前线程 === " + Thread.currentThread().getId());
//            System.out.println("任务一处理结果："+res);
//        }, executor);
//        System.out.println(future.get());

//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务一启动 === " + "当前线程 === " + Thread.currentThread().getId());
//            return 10 / 2;
//        }, executor).thenApplyAsync((result) -> {
//            System.out.println("任务二启动 === " + "当前线程 === " + Thread.currentThread().getId());
//            System.out.println("任务一处理结果：" + result);
//            return result * 2;
//        }, executor);
//        System.out.println("任务二返回值 === "+future.get());

    }

    public void main1(String[] args) throws ExecutionException, InterruptedException {
        /**
         * 1、继承Thread接口
         *         thread01 thread01 = new thread01();
         *         thread01.start();
         * 2、实现runnable接口
         *         Runnable01 runnable01 = new Runnable01();
         *         new Thread(runnable01).start();
         * 3、实现Callable接口 + FutureTask(可以拿到返回结果，可以处理异常)
         *         Callable01 callable01 = new Callable01();
         *         FutureTask<Integer> futureTask = new FutureTask<>(callable01);
         *         new Thread(futureTask).start();
         *         System.out.println("====");
         *         //等待线程执行完成后拿到返回结果
         *         System.out.println(futureTask.get());
         * 4、线程池
         *         service.execute(new Runnable01());
         *         System.out.println("====");
         */

        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                5,
                200,
                10,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(1000),
                Executors.defaultThreadFactory(),
                null
        );

    }

    public static class thread01 extends Thread {
        public void run() {
            System.out.println("当前线程 === " + Thread.currentThread().getId());
        }
    }

    public static class Runnable01 implements Runnable {
        @Override
        public void run() {
            System.out.println("当前线程 === " + Thread.currentThread().getId());
        }
    }

    public static class Callable01 implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程 === " + Thread.currentThread().getId());
            return 10 / 2;
        }
    }

}




